package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import graphic.Edge;
import graphic.Vertex;

/**
 * @author ZhangyiA
 * @describe: 生成测试样例
 * @date 2021/07/23
 */

public class DataDeal {

    @SuppressWarnings("Duplicates")
    public void dealData(String src , String dest) throws IOException {
        File srcFile = new File(src);
        File destFile = new File(dest);
        assert srcFile.exists() : String.format("File path %s isn't exists , please check it." , srcFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile)));
        String line;
        while((line = reader.readLine()) != null){
            line = line.trim();
            if(line.equals("")){
                continue;
            }
            String[] buff = line.split("\\s+");
            if(buff[0].equals("e")){
                int vertexFrom = Integer.parseInt(buff[1]);
                int vertexTo = Integer.parseInt(buff[2]);
                int label = (int) Double.parseDouble(buff[3]);
                String str = "e" + " " + vertexFrom + " " + vertexTo + " " + label;
                writer.write(str);
            } else {
                writer.write(line);
            }
            writer.flush();
            writer.newLine();
        }
        writer.close();
        reader.close();
    }

    @SuppressWarnings("Duplicates")
    public void fillFFSM(String src , String dest) throws IOException {
        File srcFile = new File(src);
        File destFile = new File(dest);
        assert srcFile.exists() : String.format("File path %s isn't exists , please check it." , srcFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile)));
        String line;
        while((line = reader.readLine()) != null){
            line = line.trim();
            if(line.equals("")){
                continue;
            }
            String[] buff = line.split("\\s+");
            if(buff[0].equals("e")){
                int vertexFrom = Integer.parseInt(buff[1]);
                int vertexTo = Integer.parseInt(buff[2]);
                int label = (int) Double.parseDouble(buff[3]);
                String str = vertexFrom + " " + vertexTo + " " + label;
                writer.write(str);
            } else if(buff[0].equals("v")){
                int nodeId = Integer.parseInt(buff[1]);
                int label = Integer.parseInt(buff[2]);
                String str =  nodeId + " " + label;
                writer.write(str);
            }
            else {
                writer.write(line);
            }
            writer.flush();
            writer.newLine();
        }
        writer.close();
        reader.close();
    }

    public void dataExpand(String srcpath , String destpath , int iter) throws IOException {
        File srcFile = new File(srcpath);
        File destFile = new File(destpath);
        assert srcFile.exists() : String.format("File path %s isn't exists , please check it." , srcFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile)));
        ArrayList<Vertex> vertices = new ArrayList<>();
        ArrayList<Edge> edges = new ArrayList<>();
        String line ;
        // 读取数据
        while((line = reader.readLine()) != null){
            line = line.trim();
            if(line.equals("")){
                continue;
            }
            String[] buff = line.split("\\s+");
            if(buff[0].equals("v")){
                int nodeId = Integer.parseInt(buff[1]);
                int label = Integer.parseInt(buff[2]);
                Vertex vertex = new Vertex(nodeId , label);
                vertices.add(vertex);
            } else if(buff[0].equals("e")){
                int vertexFrom = Integer.parseInt(buff[1]);
                int vertexTo = Integer.parseInt(buff[2]);
                int label = Integer.parseInt(buff[3]);
                Edge edge = new Edge(vertexFrom , vertexTo , label , 0);
                edges.add(edge);
            } else {
                writer.write(line);
                writer.flush();
                writer.newLine();
            }
        }

        int node_num = vertices.size();
        // 循环生成点结构
        for(int i = 0 ; i < iter ; i ++){
            int cnt = node_num * i;
            for(Vertex vertex : vertices){
                String node = "v" + " " + (vertex.getVal() + cnt) + " " + vertex.getLabel();
                writer.write(node);
                writer.flush();
                writer.newLine();
            }
        }
        // 循环生成边结构
        for(int i = 0 ; i < iter ; i ++){
            int cnt = node_num * i;
            for(Edge edge : edges){
                String edge_str = "e" + " " + (edge.getVertexFrom() + cnt) + " " + (edge.getVertexTo() + cnt) + " " + edge.getLabel();
                writer.write(edge_str);
                writer.flush();
                writer.newLine();
            }
        }

    }

    public static void main(String[] args) throws IOException {
        DataDeal dataDeal = new DataDeal();
        dataDeal.dataExpand(Constants.input , Constants.output , 50000);
//        dataDeal.fillFFSM(Constants.input , Constants.output);


    }

}
